package algo.math.evaluator.tokenizer;

import algo.math.evaluator.context.EvaluationContext;
import algo.math.evaluator.context.operator.Operator;
import algo.math.evaluator.context.operator.interfaces.IOperator;
import algo.math.evaluator.tokenizer.interfaces.*;

public class Token implements IToken {

	private TokenType _type;
	private String _value;
	
	public Token (String value,TokenType type)
	{
		_type = type;
		_value = value;
	}
	
    public Token()
    {
    	_type = TokenType.NOTHING;
    	_value = "";
    }
	
	
	@Override
	public TokenType getType() {
		return _type;
	}

	@Override
	public void  setType(TokenType type) {
		
		_type =type;
	}

	@Override
	public void setValue(String value) {
		_value = value;
	}

	@Override
	public String getValue() {
		return _value;
	}
	
	@Override
	public String toString()
	{
		return getValue();
	}
	
    public static Object resolve(String text)
    {
    	Boolean isValue = true;
    	
       // if (null !=Helper.getInstance().findOperator(text))
    	if (null !=EvaluationContext.GetInstance().getComponentById(IOperator.class, text) && text.length()==1)
        {
    		
    		Operator op = (Operator) EvaluationContext.GetInstance().getComponentById(IOperator.class, text);
        	return op;
        }
    	
    	for (int i = 0; i< text.length();i++)
    	{
    		if (!EvaluationContext.GetInstance().getBase().isBaseToken(text.charAt(i)) &&  text.charAt(i) != '-')
    		{
    			isValue = false;
    		}
    	}
    	
        if (isValue)
        {
            return (new Token(text,TokenType.CONSTANT));
        }
         
    	
    	for (int i = 0; i< text.length();i++)
    	{
    		if (Character.isDigit(text.charAt(i)) || ((text.charAt(i) == '.'))|| ((text.charAt(i) == '-')))
    		{
    			isValue = true;
    		}
    		
    		else
    			
    		{
    			isValue = false;
    			break;
    		}
    	}
        if (isValue)
        {
            return (new Token(text,TokenType.CONSTANT));
        }
         

        return (new Token(text,TokenType.VARIABLE));
    }

}
